package com.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ydd.constant.MessageConstant;
import com.ydd.dto.SysMenuDTO;
import com.ydd.entity.SysMenu;

import com.ydd.entity.YddUser;
import com.ydd.mapper.SysMenuMapper;
import com.ydd.result.Result;
import com.ydd.service.ISysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ydd.vo.MenuList;
import com.ydd.vo.MenuVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-05-15
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {
    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public Result updateMenu(SysMenuDTO dto) {
        if (dto.getId() == null) {
            return Result.error(MessageConstant.UNKNOWN_ERROR);
        }
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(dto, sysMenu);
        this.updateById(sysMenu);
        return Result.success();
    }

    @Override
    public Result del(String id) {
        if (id == null) {
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        sysMenuMapper.deleteById(id);
        return Result.success();
    }

    @Override
    public Result allData() {
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("type", 1).eq("pid", 0);
        List<SysMenu> sysMenuList = this.list(wrapper);
        List<MenuList> date = getDate(sysMenuList);
        return Result.success(date);
    }

    @Override
    public Result rowInfo(Long id) {
        SysMenu sysMenu = sysMenuMapper.selectById(id);
        MenuVO menuVO = new MenuVO();
        BeanUtils.copyProperties(sysMenu,menuVO);
        return Result.success(menuVO);
    }

    @Override
    public Result add(SysMenuDTO dto) {
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(dto,sysMenu);
        this.save(sysMenu);
        return Result.success();
    }


    public List<MenuList> getDate(List<SysMenu> sysMenuList) {
        List<MenuList> date = new ArrayList<>();
        sysMenuList.forEach(sysMenu -> {
            MenuList menuList = new MenuList();
            BeanUtils.copyProperties(sysMenu, menuList);
            LambdaQueryWrapper<SysMenu> lqw = new LambdaQueryWrapper<SysMenu>();
            lqw.eq(SysMenu::getPid, sysMenu.getId());
            List<SysMenu> list = list(lqw);
            if (list.size() == 0) {
                date.add(menuList);
                return ;
            }
            menuList.setChildren(getDate(list));
            date.add(menuList);
        });

        return date;
    }
}